package com.hzx.demo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.hzx.demo.R;
import com.hzx.demo.util.Entry;
import com.hzx.demo.util.HZXLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

/**
 * Created by ldkxingzhe on 2016/6/2.
 */
@EActivity(R.layout.activity_fragment_detach)
@Entry(desc = "测试FragmentDetach之后使用StartActivity", createTime = "2016-06-02 16:45")
public class FragmentDetachStartActivity extends AppCompatActivity{
    @SuppressWarnings("unused")
    private static final String TAG = "FragmentDetachStartActivity";

    @ViewById(R.id.fl_content)
    protected FrameLayout mContent;
    @ViewById(R.id.ll_root_view)
    protected ViewGroup mRootView;

    private MFragment mFragment;

    @AfterViews
    public void afterView(){
        mFragment = new MFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(mContent.getId(), mFragment);
        transaction.commit();
        HZXLog.v(TAG, "afterView");
    }


    @Click(R.id.btn_click_me)
    protected void onClickMe(){
        HZXLog.v(TAG, "onClickMe");
        mRootView.removeView(mContent);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                HZXLog.v(TAG, "startNewActivity");
                mFragment.startNewActivity();
            }
        }, 1000);
    }

    @Click(R.id.btn_back_click_me)
    protected void backClickMe(){
        HZXLog.v(TAG, "backClickMe");
        mRootView.addView(mContent);
    }

    public static class MFragment extends Fragment{

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            HZXLog.v(TAG, "onAttach");
        }

        @Override
        public void onDetach() {
            super.onDetach();
            HZXLog.v(TAG, "onDetach");
        }

        @Override
        public void onResume() {
            super.onResume();
            HZXLog.v(TAG, "onResume");
        }

        @Override
        public void onPause() {
            super.onPause();
            HZXLog.v(TAG, "onPause");
        }

        @Override
        public void onStop() {
            super.onStop();
            HZXLog.v(TAG, "onStop");
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            HZXLog.v(TAG, "onDestroy");
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            HZXLog.v(TAG, "onCreateView");
            TextView textView = new TextView(getActivity());
            textView.setText("Hellow world");
            return textView;
        }

        public void startNewActivity(){
            this.startActivityForResult(new Intent(getActivity(), TestActivity.class), 1);
        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            HZXLog.v(TAG, "onActivityResult, and resultCode is %d", resultCode);
            HZXLog.v(TAG, "onActivityResult, and visible is %b");
        }
    }
}
